/**
 * eCommerce Application Sample for J2EE Training
 * @author ndg
 *
 */
package shell;

import ecom.entity.Account;
//import ecom.entitycopy.AccountBean;
import ecom.entity.Category;
import ecom.exception.EcomException;
import ecom.session.EcomAdminRemote;
import ecom.session.EcomCustomerRemote;
import java.io.PrintStream;
import java.util.List;
import java.util.StringTokenizer;

public class CategoryCommandImpl implements ShellCommand {

    private ShellContext context = null;

    public CategoryCommandImpl(ShellContext context) {
        this.context = context;


    }

    public String getName() {
        return "category";
    }

    public String getUsage() {
        return "category";
    }

    public String getShortDescription() {
        String description = "Category management\n";
        description += "\t  : [without args]..................................................display all categories\n";
        if (true == context.getVar("admin")) {
            description += "\t  : -add [label] [description] ........................................create a category\n";
            description += "\t  : -remove [category id]..........................................delete a category\n";
        }
        return description;
    }

    public void execute(String cmdline, PrintStream out, PrintStream err) throws InsuffisantPrivilege {
        StringBuffer res = new StringBuffer();
        EcomCustomerRemote ecomCustomerBean = (EcomCustomerRemote) context.getVar("customerBean");
        EcomAdminRemote ecomAdminBean = (EcomAdminRemote) context.getVar("adminBean");


        StringTokenizer st = new StringTokenizer(cmdline, " ");

        // Ignore cmd
        st.nextToken();



        // without arg : list all accounts
        if (st.countTokens() == 0) {
            if (true != context.getVar("admin")) {
                throw new InsuffisantPrivilege();
            }

            for (Category category : ecomAdminBean.listCategory()) {
                System.out.println(category);
            }

        } // With option
        else {
            String option = st.nextToken();

            // adding
            if (option.equals("-add")) {
                if (true != context.getVar("admin")) {
                    throw new InsuffisantPrivilege();
                }
                //if (st.countTokens() == 3) {
                try {
                    String label;
                    String description;


                    label = st.nextToken();
                    description = st.nextToken();
                    while (st.hasMoreElements()) {
                        description += " " + st.nextToken();
                    }
                    ecomAdminBean.createCategory(label, description);

                } catch (EcomException e) {
                    err.println("Error with category creation");
                }
                //} else {
                //res.append(context.returnCode(Shell.INCORRECT_ARGS_NUMBER, RESULT_ACTION.FAILED));
                //   err.println(Shell.INCORRECT_ARGS_NUMBER);
                //}
            } // removing
            else if (option.equals("-remove")) {
                if (true != context.getVar("admin")) {
                    throw new InsuffisantPrivilege();
                }
                if (st.countTokens() == 1) {
                    try {
                        int id = Integer.parseInt(st.nextToken());
                        ecomAdminBean.deleteCategory(id);

                    } catch (Exception ex) {
                        //res.append(context.returnCode(ex.toString(), RESULT_ACTION.FAILED));
                        err.println("Error with account remove" + ex);
                    }
                } else {
                    err.println(Shell.INCORRECT_ARGS_NUMBER);
                }

            } else {
                //res.append(context.returnCode(option + " is not known for Account Management", RESULT_ACTION.FAILED));
                err.println("\t" + option + " is not known for Category Management");
            }
        }
        out.println(res.toString());
    }
}
